<template>
    <div id='' class=''>
        <Modal v-model="modalShow" width="1200" :mask-closable="false" title="字典同步">
            <Form ref="formValidate" :label-width="85" inline>
                <FormItem label="字典名称" style="width:400px">
                    <Input v-model="dictName" placeholder="字典名称"></Input>
                </FormItem>
                <FormItem label="字典代码" style="width:400px">
                    <Input v-model="dictType" placeholder="字典代码"></Input>
                </FormItem>
                <FormItem style="width:330px">
                    <Button style="margin-left:85px" type="primary" @click="seach">查询</Button>
                    <Button style="margin-left:10px" @click="ResetTable()">重置</Button>
                </FormItem>
            </Form>
            <Table row-key="id" ref="Table" :update-show-children="true" :columns="columnsTable" :data="list" @on-selection-change="handleRowChange " border style="width: 100%;">
            </Table>
            <div slot="footer">
                <Button type="text" @click="syncAll(true)">全部同步</Button>
                <Button type="text" @click="cancel">取消</Button>
                <Button type="primary" @click="syncAll(null)">同步</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import { columns } from './syncAdmin_tableConfig'
export default {
    name: '',
    props: [''],
    data() {
        return {
            dictName: '',
            dictType: '',
            columnsTable: columns,
            modalShow: false,
            syncList: [],
            syncData: [],
            list: []
        };
    },
    components: {},
    computed: {
    },
    beforeMount() { },
    mounted() { },
    methods: {
        seach() {
            this.modalShow = true
            let obj = {
                dictName: this.dictName,
                dictType: this.dictType
            }
            this.$http.$post('syncListDictionaryData', { data: obj }).then(res => {
                if (res.code == 200) {
                    this.list = res.data
                    return
                }
                this.$Message.error(res.msg);
            })
        },
        handleRowChange(selection) {
            this.syncList = selection
        },
        syncAll(status) {
            this.syncData = [];
            if (status) {
                this.$refs.Table.selectAll(status);
                let arr = []
                for (let i = 0; i < this.syncList.length; i++) {
                    arr.push(this.syncList[i].dictType)
                }
                this.syncData = this.syncData.concat(arr)
            } else {
                if (this.syncList.length === 0) {
                    this.$Message.error('请选择需要同步的数据！');
                    return
                }
                this.syncData.push(this.syncList[0].dictType)
            }
            this.$Modal.confirm({
                title: '提示信息',
                content: '同步字典将覆盖已有字典内容，是否继续同步？',
                onOk: () => {
                    this.$http.$post('syncSubmitDictionaryData', { data: this.syncData }).then(res => {
                        if (res.code == 200) {
                            this.$Message.success('同步成功');
                            this.$emit('resetTable')
                            this.cancel()
                            return
                        }
                        this.$Message.error(res.msg);
                    })
                },
                onCancel: () => {
                    this.$Message.info('已取消');
                }
            });
        },
        cancel() {
            this.modalShow = false;
            this.syncData = [];
            this.dictName = '';
            this.dictType = '';
        },
        ResetTable() {
            this.dictName = '';
            this.dictType = '';
            this.seach();
        },
    },
    watch: {}
}
</script>
<style lang='less' scoped>
</style>